#include "cmsis_os2.h"
#include "lvgl.h"


extern osMutexId_t LvglMutexHandle;

lv_obj_t* label_total = NULL;
lv_obj_t* checkbox1 = NULL;
lv_obj_t* checkbox2 = NULL;
lv_obj_t* checkbox3 = NULL;
lv_obj_t* checkbox4 = NULL;

static void checkbox_event_cb(lv_event_t *e)
{
    static int aggregate = 0;
    lv_obj_t* target = lv_event_get_target(e); /* 获取触发源 */
    {
        if(target == checkbox1)
        {
            lv_obj_has_state(checkbox1, LV_STATE_CHECKED) ? (aggregate += 19) : (aggregate -= 19);
        }
        if(target == checkbox2)
        {
            lv_obj_has_state(checkbox2, LV_STATE_CHECKED) ? (aggregate += 29) : (aggregate -= 29);
        }
        if(target == checkbox3)
        {
            lv_obj_has_state(checkbox3, LV_STATE_CHECKED) ? (aggregate += 39) : (aggregate -= 39);
        }
        if(target == checkbox4)
        {
            lv_obj_has_state(checkbox4, LV_STATE_CHECKED) ? (aggregate += 69) : (aggregate -= 69);
        }

        lv_label_set_text_fmt(label_total, "Aggregate : $%d", aggregate);                /* 更新总价格标签 */
    }
}

void StartLvglTask(void *argument)
{
    osMutexAcquire(LvglMutexHandle, osWaitForever);

    int32_t  with = lv_obj_get_width(lv_scr_act());
    int32_t  height = lv_obj_get_height(lv_scr_act());

    lv_obj_t* label_menu = lv_label_create(lv_scr_act());                               /* 创建菜单标题标签 */
    lv_label_set_text(label_menu, "MENU");                                              /* 设置文本 */
    lv_obj_set_style_text_font(label_menu, &lv_font_montserrat_24, LV_STATE_DEFAULT);   /* 设置字体 */
    lv_obj_align(label_menu, LV_ALIGN_CENTER, 0, -height * 2 / 5 );                     /* 设置位置 */
    
    label_total = lv_label_create(lv_scr_act());                                        /* 创建总价标签 */
    lv_label_set_text(label_total, "Aggregate : $0");                                   /* 设置文本 */
    lv_obj_set_style_text_font(label_total, &lv_font_montserrat_24, LV_STATE_DEFAULT);  /* 设置字体 */
    lv_obj_align(label_total, LV_ALIGN_CENTER, 0, height * 2 / 5 );                     /* 设置位置 */

    lv_obj_t *obj = lv_obj_create(lv_scr_act());                                        /* 创建基础对象作为复选框背景 */
    lv_obj_set_size(obj, with * 4 /5 , height * 3 /5);                                  /* 设置大小 */
    lv_obj_align(obj, LV_ALIGN_CENTER, 0 , 0);                                          /* 设置位置 */

    checkbox1 = lv_checkbox_create(obj);                                                /* 创建菜品 1 复选框 */
    lv_checkbox_set_text(checkbox1, "Roast chicken $19");                               /* 设置文本 */
    lv_obj_set_style_text_font(checkbox1, &lv_font_montserrat_24, LV_STATE_DEFAULT);    /* 设置字体 */
    lv_obj_align(checkbox1, LV_ALIGN_LEFT_MID, 0, -height / 5 );                        /* 设置位置 */
    
    checkbox2 = lv_checkbox_create(obj);                                                /* 创建菜品 2 复选框 */
    lv_checkbox_set_text(checkbox2, "Roast duck $29");                                  /* 设置文本 */
    lv_obj_set_style_text_font(checkbox2, &lv_font_montserrat_24, LV_STATE_DEFAULT);    /* 设置字体 */
    lv_obj_align_to(checkbox2, checkbox1, LV_ALIGN_OUT_BOTTOM_LEFT, 0, height / 16);    /* 设置位置 */

    checkbox3 = lv_checkbox_create(obj);                                                /* 创建菜品 3 复选框 */
    lv_checkbox_set_text(checkbox3, "Roast fish $39");                                  /* 设置文本 */
    lv_obj_set_style_text_font(checkbox3, &lv_font_montserrat_24, LV_STATE_DEFAULT);    /* 设置字体 */
    lv_obj_align_to(checkbox3, checkbox2, LV_ALIGN_OUT_BOTTOM_LEFT, 0, height / 16);    /* 设置位置 */

    checkbox4 = lv_checkbox_create(obj);                                                /* 创建菜品 4 复选框 */
    lv_checkbox_set_text(checkbox4, "Roast lamb $69");                                  /* 设置文本 */
    lv_obj_set_style_text_font(checkbox4, &lv_font_montserrat_24, LV_STATE_DEFAULT);    /* 设置字体 */
    lv_obj_align_to(checkbox4, checkbox3, LV_ALIGN_OUT_BOTTOM_LEFT, 0, height / 16);    /* 设置位置 */

    lv_obj_add_event_cb(checkbox1, checkbox_event_cb, LV_EVENT_VALUE_CHANGED, NULL);    /* 注册复选框菜品 1 数值改变事件 */
    lv_obj_add_event_cb(checkbox2, checkbox_event_cb, LV_EVENT_VALUE_CHANGED, NULL);    /* 注册复选框菜品 2 数值改变事件 */
    lv_obj_add_event_cb(checkbox3, checkbox_event_cb, LV_EVENT_VALUE_CHANGED, NULL);    /* 注册复选框菜品 3 数值改变事件 */
    lv_obj_add_event_cb(checkbox4, checkbox_event_cb, LV_EVENT_VALUE_CHANGED, NULL);    /* 注册复选框菜品 4 数值改变事件 */
    

    osMutexRelease(LvglMutexHandle);

    for(;;)
    {
        osMutexAcquire(LvglMutexHandle, osWaitForever);
        lv_timer_handler();
        osMutexRelease(LvglMutexHandle);
        osDelay(5);
    }
}
